import {
  defineConfig
} from 'vite'
import vue from '@vitejs/plugin-vue'
// import legacy from '@vitejs/plugin-legacy'
const path = require("path");

// https://vitejs.dev/config/

const isProduction = process.env.NODE_ENV === 'production';

export default defineConfig({
  root: process.cwd(),
  base: isProduction ? './' : './', // 如果是根目录部署请配置为 /
  mode: process.env.NODE_ENV,
  publicDir: 'public',
  plugins: [
    vue(),
    // legacy({
    //   targets: ['ie >= 11'],
    //   additionalLegacyPolyfills: ['regenerator-runtime/runtime']
    // })
  ],
  resolve: {
    alias: [{
      find: '@',
      replacement: path.join(__dirname, './src'),
    }]
  },
  server: {
    port: '8090',
    strictPort: false,
    open: false,
    cors: true,
    proxy: {
      // 字符串简写写法
      '/foo': 'http://api.hnywtjy.com',
      // 选项写法
      '/api': {
        // target: 'http://api.hnywtjy.com',
        target: 'http://ybj.tjxdezx.cn/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      // 正则表达式写法
      '^/fallback/.*': {
        // target: 'http://api.hnywtjy.com',
        target: 'http://ybj.tjxdezx.cn/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/fallback/, '')
      },
    },
  },
  build: {
    // outDir: 'dist',
    // assetsDir: 'assets',
    // sourcemap: false,
    // manifest: false,
    // brotliSize: false,

    // 清除console和debugger
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
      },
    },
    rollupOptions: {
      output: { //配置这个是让不同类型文件放在不同文件夹，不会显得太乱
        chunkFileNames: 'js/[name]-[hash].v2.js',
        entryFileNames: 'js/[name]-[hash].v2.js',
        assetFileNames: 'assets/[name]-[hash].v2.[ext]',
        manualChunks(id) { //静态资源分拆打包
          if (id.includes('node_modules')) {
            return id.toString().split('node_modules/')[1].split('/')[0].toString();
          }
          if (id.includes('echarts')) return 'vendor-echarts';

        }
      }
    },
    target: 'modules',
    outDir: 'dist/app', //指定输出路径
    assetsDir: 'assets', // 指定生成静态资源的存放路径
    minify: 'terser', // 混淆器，terser构建后文件体积更小
    emptyOutDir: true, //打包前先清空原有打包文件


  },
  optimizeDeps: {
    include: [
      "vue",
      // "vuex",
      // "vue-router",
      // "vant",
      // "axios",
    ],
  },
})